package com.lanou3g.code0515.study;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by dllo on 18/5/15.
 */
@WebServlet(name = "UploadServlet", urlPatterns = "/upload")
@MultipartConfig
// 我们也可以通过加上MultipartConfig注解
// 来获取表单中的key
// 建议在处理晚上传之后,转发到一个单独的servlet去处理表单
// 因为其他的input使用了该注解有可能导致 FileUpLoad不好用
public class UploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1.修改编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String code=request.getParameter("code");
        response.getWriter().println(code);

        //2.创建 ServletFileUpload对象,来上传文件
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        // 3.需要知道 上传的文件 要放在哪--文件夹
        String path = getServletContext().getRealPath("./") + File.separator + "upload";
        File uploadDir = new File(path);
        if (!uploadDir.exists()) {
            // 如果文件夹不存在,则创建文件夹
            uploadDir.mkdir();
        }

        // 4.
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            if (fileItems != null) {
                // 有上传的内容
                for (FileItem fileItem : fileItems) {
                    if (fileItem.isFormField()) {
                        // 如果是一个普通表单属性
                        // 就别上传了,没东西

                        // input 中的name属性
//                        String fieldName = fileItem.getFieldName();
//                        // name是文件名,当不传文件时为null
//                        String name = fileItem.getName();
//                        // string 是input的值
//                        // getString 参数传递 编码格式 解决乱码
//                        String s = fileItem.getString("utf-8");
//
//                        System.out.println("fieldName:" + fieldName);
//                        System.out.println("name:" + name);
//                        System.out.println("s:" + s);

                        continue;
                    }
                    // 这个文件名不能使用用户上传的原文件名
                    String fileName = fileItem.getName();
                    // 处理后的文件名
                    String afterName = TestUUID.handleFileName(fileName);
                    // 创建文件壳子
                    File file = new File(uploadDir, afterName);
                    fileItem.write(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }
}
